#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/vfs.h> 
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "cpfile.h"

void get_cpfile_path(char* fname, char* fpath)
{
	int ret;
	sprintf(fpath, "/mnt/app/%s", fname);
	ret = access(fpath, F_OK);
	if(ret < 0) { 
		printf("%s File not exists\n", fpath);
		sprintf(fpath, "/app/%s", fname);
		ret = access(fpath, F_OK);
		if(ret < 0) { 
			printf("--------%s File not exists!\n", fpath);
		}
	}
}

float disk_get_total_space(char* path)
{
	struct statfs sfs;
    statfs(path,&sfs); 
    return sfs.f_blocks * (sfs.f_bsize / 1024) * 1.0;
}

int disk_delete_files(const char* path)
{
	DIR * pdir;
	struct dirent *rent;
	struct stat st;
	char absolute_path[256];

	pdir = opendir(path);
	if(!pdir){
		return -ENODEV;
	}
	while (NULL != (rent = readdir(pdir))){
		if(strcmp(rent->d_name,".") == 0||strcmp(rent->d_name,"..") == 0){
			continue;
		}
		memset(absolute_path, 0, 256);
		strcat(absolute_path, path);
		strcat(absolute_path, "/");
		strcat(absolute_path, rent->d_name); 
		if(stat(absolute_path, &st) != 0){
			continue;
		}
		if(S_ISDIR(st.st_mode)){
			disk_delete_files(absolute_path);
			rmdir(absolute_path);
		}else{
			remove(absolute_path);
		}
	}
	closedir(pdir);
	return 0;
}

int user_cp(char* src, char* dst)
{
	FILE* dst_fp;
	FILE* src_fp;
	char tmp[1024];
	int length;
	src_fp = fopen(src, "rb");
	if(!src_fp){
		return -ENFILE;
	}
	dst_fp = fopen(dst, "wb");
	if(!dst_fp){
		return -ENFILE;
	}
	while(!feof(src_fp)){
		length = fread(tmp, 1, 1024, src_fp);
		fwrite(tmp, length, 1, dst_fp);
	}
	fclose(dst_fp);
	fclose(src_fp);
	return 0;
}

int user_cp_all(char* src, char* dst)
{
	DIR * pdir;
	struct dirent *rent;
	struct stat st;
	char src_absolute_path[256];
	char dst_absolute_path[256];

	pdir = opendir(src);
	if(!pdir){
		return -ENODEV;
	}
	while (NULL != (rent = readdir(pdir))){
		if(strcmp(rent->d_name,".") == 0||strcmp(rent->d_name,"..") == 0){
			continue;
		}
		memset(src_absolute_path, 0, 256);
		strcat(src_absolute_path, src);
		strcat(src_absolute_path, "/");
		strcat(src_absolute_path, rent->d_name);
		memset(dst_absolute_path, 0, 256);
		strcat(dst_absolute_path, dst);
		strcat(dst_absolute_path, "/");
		strcat(dst_absolute_path, rent->d_name); 
		if(stat(src_absolute_path, &st) != 0){
			continue;
		}
		if(S_ISDIR(st.st_mode)){
			mkdir(dst_absolute_path, 0775);
			user_cp_all(src_absolute_path, dst_absolute_path);
		}else{
			user_cp(src_absolute_path, dst_absolute_path);
		}
	}
	closedir(pdir);
	return 0;
}

int user_dd(char* src, char* dst)
{
	int dst_fd;
	FILE* src_fp;
	char tmp[1024];
	int length;
	src_fp = fopen(src, "rb");
	if(!src_fp){
		return -ENFILE;
	}
	dst_fd = open(dst, O_RDWR);
	if(dst_fd <= 0){
		return -ENODEV;
	}
	while(!feof(src_fp)){
		length = fread(tmp, 1, 1024, src_fp);
		write(dst_fd, tmp, length);
	}
	close(dst_fd);
	fclose(src_fp);
	return 0;
}
